const { contextBridge, ipcRenderer } = require('electron');

// 注入全局变量或函数
window.customVariable = 'This is a custom variable injected by preload script';
window.customFunction = function() {
  console.log('This is a custom function injected by preload script');
};
// console.log(window.customVariable); 
// window.customFunction(); 

// 记录应用打开时间并在应用关闭时获取关闭的时间, 一起发送给主进程, 然后发送给后台
const appOpenTime = new Date();

// 暴露 API 给渲染进程
contextBridge.exposeInMainWorld('appTimeTracker', {
  getAppOpenTime: () => ipcRenderer.send('app-open-time', appOpenTime.toISOString()),
});

// 监听窗口关闭事件
window.addEventListener('beforeunload', () => {
  const appCloseTime = new Date().toISOString();
  const openTime = appOpenTime.toISOString();
  // 向主进程发送打开时间和关闭时间
  ipcRenderer.send('app-close-time', { openTime, closeTime: appCloseTime });
});